package leetcode.median;

import java.util.Arrays;

/**
 * ClassName: $1049_LastStoneWeightII
 * Description:
 * date: 2022-01-27 13:21
 *
 * @author liyifan
 */
public class $1049_LastStoneWeightII {
    public int lastStoneWeightII(int[] stones) {
        int sum = Arrays.stream(stones).sum();
        int target = (int)sum / 2;
        int[] dp = new int[target + 1];
        for (int i = 0;i < stones.length;i++) {
            for (int j = target;j >= stones[i];j--) {
                dp[j] = Math.max(dp[j], dp[j - stones[i]] + stones[i]);
            }
        }
        return sum - dp[target] * 2;
    }
}
